﻿using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.NodeFormData.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAppFormRelationDAL : BasicDAL<DBFlowAppFormRelation, long>, IFlowAppFormRelationDAL
    {
        public FlowAppFormRelationDAL ( IRepository<DBFlowAppFormRelation> basicDAL ) : base(basicDAL, "app.form.relation")
        {
        }
        public void Add ( long appId, DataRelation[] adds )
        {
            DBFlowAppFormRelation[] data = adds.ConvertAll(a => new DBFlowAppFormRelation
            {
                AppId = appId,
                ColId = a.ColId,
                Id = IdentityHelper.CreateId(),
                FormId = a.FormId,
                Keywords = a.Keywords,
                ToColId = a.ToColId
            });
            this._BasicDAL.Insert(data);
        }
        public void Set ( long appId, DataRelation[] adds )
        {
            if ( adds.IsNull() )
            {
                if ( !this._BasicDAL.Delete(a => a.AppId == appId) )
                {
                    throw new ErrorException("flow.app.form.relation.delete.fail");
                }
                return;
            }
            DBFlowAppFormRelation[] datas = adds.ConvertAll(a => new DBFlowAppFormRelation
            {
                ColId = a.ColId,
                AppId = appId,
                Id = IdentityHelper.CreateId(),
                FormId = a.FormId,
                Keywords = a.Keywords,
                ToColId = a.ToColId
            });
            ISqlQueue<DBFlowAppFormRelation> queue = this._BasicDAL.BeginQueue();
            queue.Delete(a => a.AppId == appId);
            queue.Insert(datas);
            _ = queue.Submit();
        }
    }
}
